package com.ww.springboot.boot.thread.study.executor.forkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * todo
 *
 * @author wanwei
 * @since 2023-03-10 23:05
 */
public class ForkJoinDemo2 {

    private static final ExecutorService executorService = new ThreadPoolExecutor(8, 8,
            60, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

    public static void main(String[] args) throws InterruptedException {

        List<String> ss = new ArrayList<>();
        ss.add("1");
        ss.add("2");
        ss.add("3");
        ss.add("4");
        ss.add("5");
        ss.add("6");
        ss.add("7");
        ss.add("8");

        List collect = ss.stream().map(s -> (Callable) () -> call(s)).collect(Collectors.toList());


        List<Future<Object>> futures = executorService.invokeAll(collect);

        futures.forEach(future -> {
            try {
                future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }

    private static Object call(String s) {
        System.out.println(Thread.currentThread() + "---------" + s);
        return s;
    }

}
